import 'package:equatable/equatable.dart';
import 'package:website.core/core/domain/entities/web/web_one_time_purchase_item.dart';
import 'package:website.core/core/infrastructure/dtos/api/api_money_amount_dto.dart';
import 'package:website.core/core/infrastructure/dtos/api/api_web_purchase_item_dto.dart';

const kApiWebOneTimePurchaseItemTypeValue = 'one_time_purchase_item';

class ApiWebOneTimePurchaseItemDto extends Equatable implements ApiWebPurchaseItemDto {
  const ApiWebOneTimePurchaseItemDto._internal({
    required this.sku,
    required this.priceDto,
    required this.title,
    required this.legacy,
  });

  @override
  final String sku;

  @override
  final ApiMoneyAmountDto priceDto;

  @override
  final String title;

  @override
  final bool legacy;

  @override
  String get type => kApiWebOneTimePurchaseItemTypeValue;

  @override
  List<Object?> get props {
    return [
      sku,
      priceDto,
      title,
      legacy,
    ];
  }

  factory ApiWebOneTimePurchaseItemDto.fromJson(Map<String, dynamic> json) {
    final priceJson =
        Map.castFrom<dynamic, dynamic, String, dynamic>(json[kApiWebPurchaseItemPriceKey]);
    final priceDto = ApiMoneyAmountDto.fromJson(priceJson);

    return ApiWebOneTimePurchaseItemDto._internal(
      sku: json[kApiWebPurchaseItemSkuKey],
      priceDto: priceDto,
      title: json[kApiWebPurchaseItemTitleKey],
      legacy: json[kApiWebPurchaseItemLegacyKey],
    );
  }

  @override
  WebOneTimePurchaseItem toEntity() {
    return WebOneTimePurchaseItem(
      sku: sku,
      price: priceDto.toEntity(),
      title: title,
      legacy: legacy,
    );
  }
}
